from utils.handle_ini import conf
import cx_Oracle

class HandleDb:
    def __init__(self):
        #配置数据库连接参数
        orcl_info = dict(conf.items('ORCL'))
        cx_Oracle.init_oracle_client(lib_dir=r"D:\Oracle\instantclient_19_22") #Oracle客户端在本地的安装路径
        dsn = cx_Oracle.makedsn(orcl_info['host'],orcl_info['port'],orcl_info['sid'])
        username = orcl_info['username']
        password = orcl_info['password']

        #建立连接
        self.conn = cx_Oracle.connect(username,password,dsn)
        #获取游标
        self.cursor = self.conn.cursor()

    #获取一条数据
    def get_db_one_data(self,sql):
        self.cursor.execute(sql)
        return self.cursor.fetchone()

    # 获取所有数据
    def get_db_all_data(self,sql):
        self.cursor.execute(sql)
        return self.cursor.fetchall()

    #关闭游标和连接
    def close(self):
        self.cursor.close()
        self.conn.close()

orcl = HandleDb()

